//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.kingyea.esb.platform;

import com.kingyea.esb.platform.module.ModuleLoader;
import com.kingyea.esb.platform.service.DefaultModuleService;
import com.kingyea.esb.platform.service.DefaultServerInfoService;
import com.kingyea.esb.platform.service.ModuleService;
import com.kingyea.esb.platform.service.ServerInfoService;
import com.kingyea.mobilepolice.apimanage.utils.ApplicationContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

import java.io.File;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.atomic.AtomicBoolean;


public final class Platform {
    private ApplicationContext moduleLoaderApplicationContext;
    private final AtomicBoolean started;
    private final DefaultServerInfoService defaultServerInfoService;

    private Platform() {
        this.moduleLoaderApplicationContext = null;
        this.started = new AtomicBoolean(false);
        this.defaultServerInfoService = new DefaultServerInfoService();
    }

    public static Platform getPlatform() {
        return Platform.PlatformHolder.PLATFORM;
    }

    public synchronized void start() {
        if (this.started.get()) {
            throw new RuntimeException("平台已经启动");
        } else {
            String configFile = System.getProperty("KingyeaESBHome") + "/etc/platform.xml";
            File file = new File(configFile);
            if (!file.exists()) {
                throw new RuntimeException("平台模块加载配置不存在[" + file.getAbsolutePath() + "]");
            } else {
//                this.moduleLoaderApplicationContext = new FileSystemXmlApplicationContext(file.toURI().toString());
                this.moduleLoaderApplicationContext = ApplicationContextUtil.getContext();
                Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
                    private final Logger logger = LoggerFactory.getLogger("com.kingyea.esb.platform.uncaugthexception");

                    public void uncaughtException(Thread t, Throwable e) {
                        this.logger.warn("线程[" + t.getName() + "]异常", e);
                    }
                });
                ModuleLoader moduleLoader = this.getModuleLoader();
                moduleLoader.start();
                this.started.set(true);
            }
        }
    }

    public synchronized void stop() {
        this.started.set(false);
    }

    public PlatformInfoMBean getPlatformInfo() {
        Assert.notNull(this.moduleLoaderApplicationContext, "模块加载器未完成初始");
        return (PlatformInfoMBean)this.moduleLoaderApplicationContext.getBean(PlatformInfo.class);
    }

    public ModuleLoader getModuleLoader() {
        Assert.notNull(this.moduleLoaderApplicationContext, "模块加载器未完成初始");
        return (ModuleLoader)this.moduleLoaderApplicationContext.getBean(ModuleLoader.class);
    }

    public ServerInfoService getServerInfoService() {
        return this.defaultServerInfoService;
    }

    public File resolveFile(String path) {
        return this.getServerInfoService().resolve(path);
    }

    public ModuleService getModuleService() {
        return new DefaultModuleService(this.getModuleLoader());
    }

    public ApplicationContext getModuleApplicationContext() {
        return this.getModuleLoader().getModuleApplicationContext();
    }

    public static class PlatformHolder {
        static final Platform PLATFORM = new Platform();

        public PlatformHolder() {
        }
    }
}
